<script setup>
import axios from "axios";
import { ElMessage } from 'element-plus';
import { ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();

let username = ref("");
let password = ref("");
let phone = ref("");
let head_img=ref(null)
let handleFile=(event)=>{
  head_img.value=event.target.files[0]
}

//注册
const doReg =async ()=>{
  let formdata=new FormData()
  formdata.append('username',username.value)
  formdata.append('password',password.value)
  formdata.append('phone',phone.value)
  formdata.append('head_img',head_img.value)
  try{
    let res=await axios.post('/api/register',formdata,{
      headers:{
        "Content-Type":'multipart/form-data'
      }
    })
    // console.log(res.data);
    if(res.data.code===200){
      ElMessage({
        message:res.data.message,
        type:'success'
      })
      router.push('/login')
    }else if(res.data.code===102){
      ElMessage({
        message:res.data.message,
        type:'warning'
      })
    }else{
      console.log(res.data.message);
    }
  }catch(err){
    console.log(err);
  }
}
</script>

<template>
  <div class="register">
    <div class="top">
      <div class="logo-container">
        <img src="@/assets/logo3.png" alt="Logo" class="logo" />
      </div>
    </div>
    <div class="main">
      <div class="reg-page">
        <h2>
          &nbsp;&nbsp;新用户注册
          <div class="backLogin">
            已有账号？
            <a href="/login">请登录</a>
          </div>
        </h2>

        <form @submit.prevent="doReg">
          <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" required v-model="username" />
          </div>

          <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" required v-model="password" />
          </div>

          <div class="form-group">
            <label for="password">电话</label>
            <input type="number" id="phone" required v-model="phone" />
          </div>

        <div class="form-group">
          <label for="head_img">头像</label>
          <input type="file" id="head_img" required @change="handleFile"/>
        </div>          
          <a href="/" class="agreement">《知遇书坊用户注册协议及隐私政策》</a>
          <button type="submit" >立即注册</button>
        </form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register {
  font-family: "Microsoft YaHei";
  display: flex;
  flex-direction: column;
}
.top {
  width: 100%;
  height: 69px;
  margin: auto;
  overflow: hidden;
  background-color: #fff;
  border-bottom: 2px solid #e60000;
}
.logo {
  max-width: 300px;
  height: auto;
  position: absolute;
  z-index: 1000;
}

.main {
  background-color: #f6f4f5;
  width: 100%;
  height: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.tip {
  width: 600px;
  height: 30px;
  background-color: #f12a2a;
  color: #fff;
  font-size: 16px;
  padding-bottom: 6px;
}
.reg-page {
  width: 1000px;
  height: 460px;
  margin: 20px auto;
  padding: 40px 200px 20px 200px;
  /* text-align: center; */
  background-color: #fff;
}

.logo-container {
  margin-bottom: 20px;
  /* text-align: center; */
}

h2 {
  border-left: 3px solid red;
  margin-bottom: 50px;
  font-weight: bold;
}

.form-group {
  display: flex;
  align-items: center;
  margin: 30px 20px;
}

label {
  margin-right: 10px;
  width: 70px;
}

input {
  width: 300px;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

button {
  height: 50px;
  line-height: 50px;
  border-radius: 5px;
  width: 380px;
  text-align: center;
  text-decoration: none;
  background: #e60000;
  font-size: 16px;
  color: #fff;
  letter-spacing: 3px;
  border: none;
  margin-left: 50px;
}

button:hover {
  background-color: #e20000;
}

.agreement {
  display: block;
  color: #000;
  font-size: 14px;
  text-decoration: none;
  margin-left: 50px;
  margin-bottom: 20px;
}
.agreement:hover {
  color: #e20000;
}

.backLogin {
  float: right;
  color: #c1c1c1;
  font-size: 14px;
  padding-top: 5px;
}
.backLogin a {
  color: #999999;
  text-decoration: none;
}
</style>
